from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from rest_framework import status
from rest_framework.test import APITestCase
from .models import Club, Activity, ClubMember
from users.models import User


class ClubModelTest(TestCase):
    """测试社团模型"""
    
    def setUp(self):
        self.user = User.objects.create_user(
            username='clubadmin',
            email='club@example.com',
            password='clubpassword123'
        )
        
        self.club = Club.objects.create(
            name='测试社团',
            description='这是一个测试社团',
            category='学术',
            status='active',
            contact_person='联系人',
            contact_phone='13800138000',
            founded_date=timezone.now().date(),
            is_verified=True
        )
        
        # 创建社团成员并设置为会长
        self.club_member = ClubMember.objects.create(
            club=self.club,
            user=self.user,
            role='president'
        )
    
    def test_club_creation(self):
        """测试社团创建"""
        self.assertEqual(self.club.name, '测试社团')
        self.assertEqual(self.club.category, '学术')
        self.assertEqual(self.club.status, 'active')
        self.assertTrue(self.club.created_at)
        # 测试会长关系
        president = self.club.members.filter(role='president').first()
        self.assertEqual(president.user, self.user)


class ActivityModelTest(TestCase):
    """测试活动模型"""
    
    def setUp(self):
        self.user = User.objects.create_user(
            username='activityadmin',
            email='activity@example.com',
            password='activitypassword123'
        )
        
        self.club = Club.objects.create(
            name='测试社团',
            description='这是一个测试社团',
            category='学术',
            status='active',
            contact_person='联系人',
            contact_phone='13800138000',
            founded_date=timezone.now().date(),
            is_verified=True
        )
        
        # 创建社团成员并设置为会长
        ClubMember.objects.create(
            club=self.club,
            user=self.user,
            role='president'
        )
        
        self.activity = Activity.objects.create(
            title='测试活动',
            description='这是一个测试活动',
            club=self.club,
            organizer=self.user,
            activity_type='academic',
            start_time='2023-12-01 10:00:00',
            end_time='2023-12-01 12:00:00',
            registration_deadline='2023-11-30 23:59:59',
            location='测试地点',
            max_participants=50,
            status='published'
        )
    
    def test_activity_creation(self):
        """测试活动创建"""
        self.assertEqual(self.activity.title, '测试活动')
        self.assertEqual(self.activity.club, self.club)
        self.assertEqual(self.activity.organizer, self.user)
        self.assertEqual(self.activity.status, 'published')
        self.assertEqual(self.activity.max_participants, 50)


class ActivityAPITest(APITestCase):
    """测试活动API"""
    
    def setUp(self):
        # 创建测试用户
        self.user = User.objects.create_user(
            username='activityuser',
            email='activityuser@example.com',
            password='activityuser123'
        )
        
        # 创建测试社团
        self.club = Club.objects.create(
            name='API测试社团',
            description='这是一个API测试社团',
            category='学术',
            status='active',
            contact_person='联系人',
            contact_phone='13800138000',
            founded_date=timezone.now().date(),
            is_verified=True
        )
        
        # 创建社团成员并设置为会长
        ClubMember.objects.create(
            club=self.club,
            user=self.user,
            role='president'
        )
        
        # 创建测试活动
        self.activity = Activity.objects.create(
            title='API测试活动',
            description='这是一个API测试活动',
            club=self.club,
            organizer=self.user,
            activity_type='academic',
            start_time='2023-12-01 10:00:00',
            end_time='2023-12-01 12:00:00',
            registration_deadline='2023-11-30 23:59:59',
            location='API测试地点',
            max_participants=50,
            status='published'
        )
        
        # API endpoints
        self.club_list_url = reverse('club-list')
        self.club_detail_url = reverse('club-detail', args=[self.club.id])
        self.activity_list_url = reverse('activity-list')
        self.activity_detail_url = reverse('activity-detail', args=[self.activity.id])
        self.activity_registration_url = reverse('activity-registration')
    
    def test_club_list(self):
        """测试社团列表API"""
        response = self.client.get(self.club_list_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['message'], '获取社团列表成功')
        self.assertEqual(len(response.data['data']), 1)
        self.assertEqual(response.data['data'][0]['name'], 'API测试社团')
    
    def test_club_detail(self):
        """测试社团详情API"""
        response = self.client.get(self.club_detail_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['message'], '获取社团详情成功')
        self.assertEqual(response.data['data']['name'], 'API测试社团')
        self.assertEqual(response.data['data']['description'], '这是一个API测试社团')
    
    def test_activity_list(self):
        """测试活动列表API"""
        response = self.client.get(self.activity_list_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['data']['count'], 1)
        self.assertEqual(len(response.data['data']['list']), 1)
        self.assertEqual(response.data['data']['list'][0]['title'], 'API测试活动')
    
    def test_activity_detail(self):
        """测试活动详情API"""
        response = self.client.get(self.activity_detail_url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['message'], '获取活动详情成功')
        self.assertEqual(response.data['data']['title'], 'API测试活动')
        self.assertEqual(response.data['data']['location'], 'API测试地点')
    
    def test_activity_registration(self):
        """测试活动报名API"""
        # 先登录
        self.client.force_authenticate(user=self.user)
        
        # 确保活动状态为报名中
        self.activity.status = 'registration_open'
        self.activity.save()
        
        # 发送包含活动ID和必要字段的POST数据
        response = self.client.post(self.activity_registration_url, {
            'activity': self.activity.id,
            'contact_phone': '13800138000',
            'special_requirements': '无特殊要求'
        })
        print('响应内容:', response.data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['message'], '报名成功')


# 更多测试用例可以根据实际API添加
# 例如: 测试社团成员管理、活动筛选等
